<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>网络分析服务-多旅行商分析(质检巡查)</title>

    <link href="http://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
    <link href="http://cdn.bootcss.com/openlayers/4.2.0/ol.css" rel="stylesheet">
</head>
<body style=" margin: 0;overflow: hidden;background: #fff;width: 100%;height:100%">
<div id="map" style="width: 100%;height:100%"></div>
<div id="stopMarker1">
    <img src="http://support.supermap.com.cn:8090/iserver/iClient/forJavaScript/theme/images/marker.png"/>
</div>
<div id="stopMarker2">
    <img src="http://support.supermap.com.cn:8090/iserver/iClient/forJavaScript/theme/images/marker.png"/>
</div>
<div id="stopMarker3">
    <img src="http://support.supermap.com.cn:8090/iserver/iClient/forJavaScript/theme/images/marker.png"/>
</div>

<script src="http://cdn.bootcss.com/openlayers/4.2.0/ol.js"></script>
<script type="text/javascript" src="../../dist/iclient9-openlayers.min.js"></script>
<script type="text/javascript">
    var map,
        baseUrl = "http://support.supermap.com.cn:8090/iserver/services/map-changchun/rest/maps/长春市区图",
        serviceUrl = "http://support.supermap.com.cn:8090/iserver/services/transportationanalyst-sample/rest/networkanalyst/RoadNet@Changchun";
    //多旅行商分析是无序的路径分析
    var extent = [48.4, -7668.25, 8958.85, -55.58];
    var projection = new ol.proj.Projection({
        extent: extent,
        units: 'm'
    });
    new ol.supermap.MapService(baseUrl).getMapInfo(function (serviceResult) {
        var mapJSONObj = serviceResult.result;
        map = new ol.Map({
            target: 'map',
            controls: ol.control.defaults({attributionOptions: {collapsed: false}})
                .extend([new ol.supermap.control.Logo()]),
            view: new ol.View({
                center: [5000, -3700],
                zoom: 2,
                projection: projection
            })
        });
        var options = ol.source.TileSuperMapRest.optionsFromMapJSON(baseUrl, mapJSONObj);
        var layer = new ol.layer.Tile({
            source: new ol.source.TileSuperMapRest(options)
        });
        map.addLayer(layer);
        findTSPPathsProcess();
    });

    function findTSPPathsProcess() {
        //添加站点
        var stopMarker1 = new ol.Overlay({
            element: document.getElementById('stopMarker1'),
            positioning: 'center-center',
            position: [3000, -1000]
        });
        var stopMarker2 = new ol.Overlay({
            element: document.getElementById('stopMarker2'),
            positioning: 'center-center',
            position: [3760, -4850]
        });
        var stopMarker3 = new ol.Overlay({
            element: document.getElementById('stopMarker3'),
            positioning: 'center-center',
            position: [8000, -2700]
        });
        map.addOverlay(stopMarker1);
        map.addOverlay(stopMarker2);
        map.addOverlay(stopMarker3);

        //创建多旅行商分析参数实例
        var resultSetting = new SuperMap.TransportationAnalystResultSetting({
            returnEdgeFeatures: true,
            returnEdgeGeometry: true,
            returnEdgeIDs: true,
            returnNodeFeatures: true,
            returnNodeGeometry: true,
            returnNodeIDs: true,
            returnPathGuides: true,
            returnRoutes: true
        });
        var analystParameter = new SuperMap.TransportationAnalystParameter({
            resultSetting: resultSetting,
            weightFieldName: "length"
        });
        var findTSPPathsParameter = new SuperMap.FindTSPPathsParameters({
            //是否指定终止点
            endNodeAssigned: false,
            isAnalyzeById: false,
            //旅行商分析途经点数组，必设字段
            nodes: [new ol.geom.Point([3000, -1000]), new ol.geom.Point([3760, -4850]), new ol.geom.Point([8000, -2700])],
            parameter: analystParameter
        });
        //进行查找
        new ol.supermap.NetworkAnalystService(serviceUrl).findTSPPaths(findTSPPathsParameter, function (serviceResult) {
            serviceResult.result.tspPathList.map(function (result) {
                var vectorSource = new ol.source.Vector({
                    features: (new ol.format.GeoJSON()).readFeatures(result.route)
                });
                var resultLayer = new ol.layer.Vector({
                    source: vectorSource,
                    style: new ol.style.Style({
                        stroke: new ol.style.Stroke({
                            color: 'rgba(100, 100, 225, 10)',
                            width: 3
                        }),
                        fill: new ol.style.Fill({
                            color: 'rgba(0, 0, 255, 0.1)'
                        })
                    })
                });
                map.addLayer(resultLayer);
            });
        });
    }
</script>
</body>
</html>